{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1_mkdatasetdir.sh\n",
      "2_movedata.sh\n",
      "3_shuffle_and_rename.sh\n",
      "class_01\n",
      "class_02\n",
      "class_03\n",
      "class_04\n",
      "class_05\n",
      "class_06\n",
      "class_07\n",
      "class_08\n",
      "class_09\n",
      "class_10\n"
     ]
    }
   ],
   "source": [
    "#此py文件用于生成train.list和test.list，可控制数据集每个类别的样本基数，可控制训练和测试样本的比例\n",
    "#用法示例\n",
    "#create_train_test_list(\"/home/wjj/HDD/HDD/UCF-101/\",\"train.list\",\"test.list\")\n",
    "#其中，0.7是训练样本占总体样本的默认比例，50是默认的总体样本基数，可自行选择\n",
    "import os\n",
    "def create_train_test_list(basepath,train_list,test_list,video_number=50,train_factor=0.7):    #basepath是数据集的路径，inputlist文件名 outputprefix文件名，帧采样间隔\n",
    "    f_train_list = open(train_list,'w')    #打开文件准备写入\n",
    "    f_test_list = open(test_list,'w')    #打开文件准备写入\n",
    "    classlist =  os.listdir(basepath)    #将basepath下的文件遍历并存入classlist\n",
    "    label = 0  \n",
    "    classlist.sort()    #按照类别的首字母排序\n",
    "    for classvar in classlist:    #for数据集下的每个类    \n",
    "        videodir = os.path.join('%s%s' % (basepath,classvar))\n",
    "        print(classvar)\n",
    "        if os.path.isdir(videodir) == True:    #判断该条目是否是文件夹\n",
    "            sample_number = 0\n",
    "            videolist = os.listdir(videodir)    #将video条目存入videolist\n",
    "            videolist.sort()\n",
    "            #number_of_samples = len(videolist)\n",
    "            number_of_samples = video_number        #样本基数为自定义数量\n",
    "            train_samples_number = int(train_factor *  number_of_samples)    #控制训练样本的数量\n",
    "            #test_samples_number = int((1 - train_factor) * number_of_samples)    #控制测试样本的数量\n",
    "            #print(train_samples_number,test_samples_number)\n",
    "            for videovar in  videolist:    #for每个类下的每个video\n",
    "                clipdir = os.path.join('%s/%s' % (videodir,videovar))\n",
    "                if os.path.isdir(clipdir) == True:\n",
    "                    if sample_number <= train_samples_number:    #train\n",
    "                        #print(\"train\",clipdir,label)\n",
    "                        f_train_list.write('%s %s\\n' % (clipdir,label))\n",
    "                    else :\n",
    "                        #print(\"test\",clipdir,label)  \n",
    "                        f_test_list.write('%s %s\\n' % (clipdir,label))\n",
    "                    sample_number += 1\n",
    "                    if sample_number >= number_of_samples:\n",
    "                        break                               \n",
    "            label=label+1\n",
    "    f_test_list.close()        \n",
    "    f_train_list.close()\n",
    "\n",
    "create_train_test_list(\"/samba/HDD/dataset_ucf101/\",\"train_ucf101.lst\",\"test_ucf101.lst\",40,0.7)\n",
    "#create_train_test_list(\"/home/wjj/HDD/HDD/UCF-101/\",\"train.list\",\"test.list\",[每类总样本数],[训练样本所占比例])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
